package com.arch.dev.server;

import java.io.File;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletContext;

import org.apache.log4j.Logger;

import com.arch.dev.client.ExcelService;
import com.arch.dev.client.vo.BudgetVO;
import com.arch.dev.client.vo.ExcelVO;
import com.arch.dev.server.util.BudgetCalcUtil;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class ExcelServiceImpl extends RemoteServiceServlet implements
		ExcelService {

	Logger logger = Logger.getLogger(ExcelServiceImpl.class);	
	/**
	 * 
	 */
	private static final long serialVersionUID = -5013864398619135196L;

	@Override
	public ExcelVO generateExcel(List<BudgetVO> budgetVOs) throws Exception {

		Calendar cl = Calendar.getInstance();

		Date date = cl.getTime();

		Long time = cl.getTimeInMillis();

		String excelName = "CostExpenditure";// + time;

		ServletContext sc = this.getServletContext();

		logger.info("*********" + sc.getRealPath("/"));

		File directory = new File(sc.getRealPath("/") + "Excel");

		if (!directory.exists()) {

			directory.mkdir();

		}

		File file = new File(directory, excelName + ".xls");

		logger.info("*****" + file.getAbsolutePath());

		BudgetCalcUtil.convertToExcel(file, budgetVOs);

		ExcelVO excelVO = new ExcelVO();

		excelVO.setDate(date);

		excelVO.setName(excelName);

		return excelVO;
	}

}
